<?php

namespace EShop\Controller;

/**
 * ProductController 类
 *
 * @since VER:1.0; DATE:2016-8-29; AUTHOR:SoChishun; EMAIL:14507247@qq.com; DESC:Added.
 */
class ProductController extends AppbaseController {

    function products($cid = 0) {
        if(!is_numeric($cid)){
            $cid=0;
        }
        $m_cat = new \XWAM\Model\ProductCategoryModel();
        $cids = $m_cat->get_child_ids($cid);
        $abreadcrumb = $m_cat->get_parent_list($cid); // 当前分类以及所有父分类列表
        unset($m_cat);

        $m_product = new \XWAM\Model\ProductGoodsModel();
        $list = $m_product->get_paging_list($page, array('where' => array('category_id' => array('in', $cids)), 'field' => 'id, title, picture_url, market_price, price', 'order' => 'id desc'), array('cid' => $cid));
        unset($m_product);
        $this->assign('cid', $cid);
        $this->assign('abreadcrumb', $abreadcrumb);
        $this->assign('list', $list);
        $this->assign('page', $page->show());
        $this->display();
    }

    function product($id = 0) {
        if (!$id || !is_numeric($id)) {
            $this->error('参数无效!');
        }
        // 获取商品
        $m_product = new \XWAM\Model\ProductGoodsModel();
        $data = $m_product->find($id);
        if ($data) {
            $data['attr_table'] = $m_product->get_content_text($id, 'attr_table');
            $data['description'] = $m_product->get_content_text($id, 'description');
            // 更新计数器
            $m_product->where(array('id' => $id))->setInc('visit_count');
            // 加入浏览历史
        }
        unset($m_product);
        if (!$data) {
            $this->error('商品不存在!');
        }

        $m_cat = new \XWAM\Model\ProductCategoryModel();
        $abreadcrumb = $m_cat->get_parent_list($data['category_id']); // 当前分类以及所有父分类列表
        unset($m_cat);
        $m_photos = new \XWAM\Model\ProductAlbumModel();
        $thumbs = $m_photos->where(array('product_id' => $id))->field('title, file_path')->select();
        unset($m_photos);
        $this->assign('thumbs', $thumbs);
        $this->assign('abreadcrumb', $abreadcrumb);
        $this->assign('data', $data);
        $this->display();
    }

    function add_favorite($pid=0) {
        if(!$pid || !is_numeric($pid)){
            exit('参数错误');
        }
        if (!user_check_login($data)) {
            $this->ajaxReturn(array('status' => false, 'info' => '未登录!'));
        }
        $m_fav = new \XWAM\Model\ProductFavoriteModel();
        $result = $m_fav->add_favorite($pid, $data['user_name']);
        $this->ajaxReturn($result);
    }

}
